package com.bsj.power.pls.web;

import com.alibaba.excel.EasyExcelFactory;
import com.bsj.power.common.config.exception.ApiException;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.config.global.JsonResultEnum;
import com.bsj.power.common.def.dto.cardReport.CardReportSearchDTO;
import com.bsj.power.common.def.dto.cardReport.PersonalCardReportDTO;
import com.bsj.power.common.def.vo.pls.BindCardStatisticsVO;
import com.bsj.power.common.def.vo.pls.CardReportVO;
import com.bsj.power.common.def.vo.pls.PersonalCardReportInfoVO;
import com.bsj.power.common.def.vo.pls.excel.CardReportExcelVO;
import com.bsj.power.common.util.DateUtils;
import com.bsj.power.pls.service.CardReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author LiJiaXin
 * @version 1.0.0
 * @description CardReportMapper
 * @time 2024/4/23 19:13
 */
@RestController
@RequestMapping("/cardReport")
//@SaCheckPermission("/pls/cardReport")
public class CardReportController {

    @Autowired
    private CardReportService cardReportService;

    /**
     * 智慧领卡报表
     *
     * @param cardReportSearchDTO
     * @return com.bsj.power.common.config.global.JsonResult<java.util.List < com.bsj.power.common.def.vo.pls.CardReportVO>>
     * @author ljx
     * @time 2024/4/23 20:09
     */
    @GetMapping("/page")
    public JsonResult<List<CardReportVO>> page(@Validated CardReportSearchDTO cardReportSearchDTO) {
        return cardReportService.pageCardReport(cardReportSearchDTO);
    }

    /**
     * 智慧绑卡记录导出
     *
     * @param response
     * @param cardReportSearchDTO
     * @return void
     * @author ljx
     * @time 2024/5/10 16:23
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response, CardReportSearchDTO cardReportSearchDTO) throws IOException {
        List<CardReportExcelVO> cardReportExcelVOS = cardReportService.getExportData(cardReportSearchDTO);
        if (CollectionUtils.isEmpty(cardReportExcelVOS)) {
            throw new ApiException(JsonResultEnum.NO_EXPORT_DATA);
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //编码 防止中文乱码
        String fileName = URLEncoder.encode("智慧绑卡记录" + DateUtils.dateToStr(DateUtils.TIME_STANDARD_FORMAT2, new Date()), "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        EasyExcelFactory.write(response.getOutputStream(), CardReportExcelVO.class).sheet("智慧绑卡记录")
                .doWrite(cardReportExcelVOS);
    }

    private static long sevenDayTimeMillions = 1000 * 60 * 60 * 24 * 30L;

    /**
     * 查询人员的绑卡信息
     *
     * @param dto
     * @return
     */
    @PostMapping("/listPersonalCardReportInfo")
    public JsonResult<List<PersonalCardReportInfoVO>> listPersonalCardReportInfo(@RequestBody PersonalCardReportDTO dto) {
        // 查询时间不要超过七天
        if (Math.abs(DateUtils.strToDate(dto.getEndTime()).getTime()
                - DateUtils.strToDate(dto.getBeginTime()).getTime()) > sevenDayTimeMillions) {
            JsonResult jsonResult = new JsonResult();
            jsonResult.setCode(-1);
            jsonResult.setMsg("查询时间不能超过30天");
            return jsonResult;
        }

        List<PersonalCardReportInfoVO> list = cardReportService.listPersonalCardReportInfo(dto);

        JsonResult jsonResult = JsonResult.success();
        if (!CollectionUtils.isEmpty(list)) {
            jsonResult.setData(list);
            return jsonResult;
        }
        return jsonResult;
    }

    /**
     * 绑卡信息相关统计
     *
     * @return com.bsj.power.common.config.global.JsonResult<com.bsj.power.common.def.vo.pls.BindCardStatisticsVO>
     * @author ljx
     * @time 2024/12/18 19:32
     */
    @GetMapping("/statistics")
    public JsonResult<BindCardStatisticsVO> statistics() {
        return JsonResult.success(cardReportService.statistics());
    }

    /**
     * 绑卡趋势
     *
     * @param startTime
     * @param endTime
     * @param type      1月统计 2日统计
     * @return com.bsj.power.common.config.global.JsonResult<java.util.Map < java.lang.String, java.lang.Long>>
     * @author ljx
     * @time 2024/12/19 16:09
     */
    @GetMapping("/listNumberByTime")
    public JsonResult<Map<String, Long>> listNumberByTime(String startTime, String endTime, Integer type) throws ParseException {
        return JsonResult.success(cardReportService.listNumberByTime(startTime, endTime, type));
    }

}
